import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import '../../resources/kq_theme_colors.dart';
import 'package:kq_flutter_widgets/widgets/button/kq_underline_button.dart';

import '../../theme/kq_theme_manager.dart';
import '../divider/kq_divider.dart';
import 'entity/kq_form_entity.dart';

//展示附件信息
class KqAttachmentView extends StatefulWidget {
  /// 通用属性
  final KqFormEntity entity; //extraData:[KqAttachmentEntity]

  /// 控件不可编辑时点击回调
  final Function(KqFromAttachEntity item, int index)? onTap;

  const KqAttachmentView({Key? key, required this.entity, this.onTap})
      : super(key: key);

  @override
  KqAttachmentViewState createState() => KqAttachmentViewState();
}

class KqAttachmentViewState extends State<KqAttachmentView> {
  @override
  void initState() {
    widget.entity.widgetState = this;
    super.initState();
  }

  List<Widget> renderAnnex() {
    List<Widget> forms = <Widget>[];
    if (widget.entity.extraData != null &&
        (widget.entity.extraData as List).isNotEmpty) {
      double hor = widget.entity.paddingHorizontal ??
          KqThemeManager.instance.getConfig().formConfig.paddingHorizontal ??
          16.r;
      int count = (widget.entity.extraData as List).length;
      for (var i = 0; i < (widget.entity.extraData as List).length; i++) {
        KqFromAttachEntity e = widget.entity.extraData[i];
        double bottom = count == i + 1
            ? widget.entity.paddingVertical ??
                KqThemeManager.instance
                    .getConfig()
                    .formConfig
                    .paddingVertical ??
                15.r
            : 6.r;
        forms.add(Container(
          padding:
              EdgeInsets.only(left: hor, right: hor, top: 6.r, bottom: bottom),
          child: KqUnderlineButton(
            title: e.fileName ?? e.attachmentName ?? '',
            textAlign: TextAlign.start,
            onTap: () {
              if (widget.onTap != null) {
                widget.onTap!(e, i);
              }
              if (e.onTap != null) {
                e.onTap!(e, i);
              }
            },
          ),
        ));
      }
    } else {
      forms.add(Container());
    }
    return forms;
  }

  //头部
  Widget renderHead() {
    double ver = widget.entity.paddingVertical ??
        KqThemeManager.instance.getConfig().formConfig.paddingVertical ??
        15.r;
    double hor = widget.entity.paddingHorizontal ??
        KqThemeManager.instance.getConfig().formConfig.paddingHorizontal ??
        16.r;
    return Container(
      padding: EdgeInsets.only(
          bottom: widget.entity.extraData == null ||
                  (widget.entity.extraData as List).isEmpty
              ? ver
              : 0,
          top: ver,
          left: hor,
          right: hor),
      child: Text(
        widget.entity.title,
        maxLines: widget.entity.multiline ? null : 1,
        overflow: widget.entity.multiline ? null : TextOverflow.ellipsis,
        style: widget.entity.titleStyle ??
            TextStyle(
                fontSize: KqThemeManager.instance
                        .getConfig()
                        .formConfig
                        .titleFontSize ??
                    16.sp,
                color: KqThemeColors.text26),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    // 如果外层实体是反复重新创建的，则需要在此重新赋值
    widget.entity.widgetState = this;
    return Container(
      color: Colors.white,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [renderHead(), ...renderAnnex(), const KqDivider()],
      ),
    );
  }
}
